package metu.tespit.session.course;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import metu.tespit.entitiy.accountManagement.User;
import metu.tespit.entity.course.Course;
import metu.tespit.session.accountManagement.UserConditionalList;
import metu.tespit.session.sessionScopeManager.SessionScopeBean;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.web.RequestParameter;


/**
 
 * @created May 23, 2012
 */
@Name("studentForCourseList")
@Scope(ScopeType.CONVERSATION)
public class StudentForCourseList{

	
	private Long courseId;
	
	@In(create=true)
	private UserConditionalList userConditionalList;

	private Long teamId;
	
	public Long getTeamId() {
		return teamId;
	}

	public void setTeamId(Long teamId) {
		this.teamId = teamId;
	}


	@In
	private EntityManager entityManager;
	
	public List<User> getResultList(){
		
		Course c=(Course) entityManager.createQuery("Select cl from Course cl where cl.id=:id").setParameter("id", courseId).getSingleResult();
		
		List<User> a=userConditionalList.getStudentList();
		
		List<User> returnList=new ArrayList<User>();
		
		for(User us : a){
			
			if(!us.getCoursesTaken().contains(c)){
				returnList.add(us);
			}
			
		}
		return a;
		
	}

	public List<User> getResultListForTeams(){
		
		Course c=(Course) entityManager.createQuery("Select cl from Course cl where cl.id=:id").setParameter("id", courseId).getSingleResult();
		
		List<User> a=userConditionalList.getStudentList();
		
		List<User> returnList=new ArrayList<User>();
		
		for(User us : a){
			
			if(us.getCoursesTaken().contains(c)){
				if(us.getProjectTeam()==null)
					returnList.add(us);
				else if(teamId!= null && us.getProjectTeam().getId()==teamId){
					returnList.add(us);
				}
				
			}
			
		}
		return returnList;
		
	}
	

	public Long getCourseId() {
		return courseId;
	}

	public void setCourseId(Long courseId) {
		this.courseId = courseId;
	}

	
	//TODO: time restrictions
	

	@In(scope=ScopeType.SESSION)
	private SessionScopeBean sessionScopeBean;

	public SessionScopeBean getSessionScopeBean() {
		return sessionScopeBean;
	}


	

	
}
